<?php

/**
 * Class IndexController
 */
header("Content-type:text/html;charset=utf-8");
class ShopController extends BaseController
{
    //——————————————————————————央票通商城————————————————————————

//    /**
//     * 央票通商城首页
//     * @return mixed
//     */
//    public function YptCommonIndex()
//    {
//        print_r("12312");
//
//        return $this->view('common.shop_index_head');
//    }

    /**
     * 央票通商家列表
     * @return mixed
     */
    public function YptBusinessList()
    {
        $info = ShopService::instance()->YptBusinessList();
        return $this->view('shop.ypt_s_business_list')->with('info',$info);
    }
    /**
     *央票通商家商品列表
     * @return mixed
     */
    public function YptBusinessShopList()
    {
        $business_id = $this->getParam('business_id');
        $c_id = $this->getParam('c_id');

        $info = ShopService::instance()->YptBusinessShopList($business_id,$c_id);
        $sort = ShopService::instance()->YptShopSort($business_id);
        $sort = json_encode($sort);
        $sort = json_decode($sort,true);
        $arr = '';
        foreach($sort as $k=>$v){
           $arr[] = $v['c_id'];
        }
        $arr = array_unique($arr);
        foreach($sort as $k=>$v){
            foreach($arr as $kk=>$vv){
                if($k!=$kk){
                    if($v['c_id']==$vv){
                        unset($sort[$k]);
                    }
                }
            }
        }

        return $this->view('shop.ypt_s_business_shop_list')->with(['bus_info'=>$info['data1'],'shop_list'=>$info['data2'],'business_id'=>$business_id,'sort'=>$sort]);
    }

    /**
     *央票通商品详情
     * @return mixed
     */
    public function YptShopDetails()
    {
        $product_id = $this->getparam('product_id');
        $info = ShopService::instance()->YptShopDetails($product_id);
        $spec = ShopService::instance()->YptShopDetailsSpec($info->sort);
        return $this->view('shop.ypt_s_shop_details')->with(['info'=>$info,'spec'=>$spec]);
    }

    public function YptShopDetailsJS()
    {
        $product_id = $this->getparam('product_id');
        $info = ShopService::instance()->YptShopDetails($product_id);
        $spec = ShopService::instance()->YptShopDetailsSpec($info->sort);
        return $spec;
    }

    /**
     *央票通商品列表
     * @return mixed
     */
    public function YptShopList()
    {
        $id             = $this->getParam('id');
        $keywords       = $this->getParam('keywords');
        $sort           = $this->getParam('c_id');
        $business       = $this->getParam('business');
        $stock          = $this->getParam('stock');
        $mix_price      = $this->getParam('mix_price');
        $max_price      = $this->getParam('max_price');

//        echo $id;
//        echo $sort;
//        echo $stock ;
//        echo $mix_price;
//        echo $max_price;die;
        if(!empty($sort)){
            $c_id = ShopService::instance()->YptShopListSort($sort);
            $c_id = substr($c_id,0,strlen($c_id)-1);
            if(!empty($business)){
                $info = ShopService::instance()->YptShopBusiness($c_id);
                $arr = array();
                foreach($info as $k=>$v){
                    $arr[] = $v->business_id;
                }
                $arr = array_unique($arr);
                $array = ShopService::instance()->YptShopBusinessList($arr);
                return $this->view('shop.ypt_s_business_list')->with(['info'=>$array,'sort'=>$sort,'business'=>$business]);
            }
            if(!empty($id)){
                $info = ShopService::instance()->YptShopListOrderSortSearch($c_id,$id,$stock,$mix_price,$max_price);
                return $this->view('shop.ypt_s_shop_list')->with(['info'=>$info['data'],'id'=>$info['id'],'sort'=>$sort,'stock'=>$stock,'mix_price'=>$mix_price,'max_price'=>$max_price]);
            }else{
                $info = ShopService::instance()->YptShopListSortSearch($c_id,$stock,$mix_price,$max_price);
                return $this->view('shop.ypt_s_shop_list')->with(['info'=>$info['data'],'sort'=>$sort,'stock'=>$stock,'mix_price'=>$mix_price,'max_price'=>$max_price]);
            }
        }

        if(empty($keywords)){
            if(empty($id)&&empty($stock)&&empty($mix_price)&&empty($max_price)){
                $info = ShopService::instance()->YptShopList();
                return $this->view('shop.ypt_s_shop_list')->with('info',$info);
            }else{
                $info = ShopService::instance()->YptShopListOrder($id,$stock,$mix_price,$max_price);
                return $this->view('shop.ypt_s_shop_list')->with(['info'=>$info['data'],'id'=>$info['id'],'stock'=>$stock,'mix_price'=>$mix_price,'max_price'=>$max_price]);
            }
        }else{
            if(empty($id)){
                $info = ShopService::instance()->YptShopListSearch($keywords,$stock,$mix_price,$max_price);
                return $this->view('shop.ypt_s_shop_list')->with(['info'=>$info['data'],'keywords'=>$info['keywords'],'stock'=>$stock,'mix_price'=>$mix_price,'max_price'=>$max_price]);
            }else{
                $info = ShopService::instance()->YptShopListOrderSearch($keywords,$id,$stock,$mix_price,$max_price);
                return $this->view('shop.ypt_s_shop_list')->with(['info'=>$info['data'],'id'=>$info['id'],'keywords'=>$info['keywords'],'stock'=>$stock,'mix_price'=>$mix_price,'max_price'=>$max_price]);
            }
        }


    }



    /**
     * 购物车
     * @return mixed
     */
    public function YptShopCart()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);

        $info = ShopService::instance()->YptShopCart($user_id);
        $count = ShopService::instance()->YptShopCartCount($user_id);
        return $this->view('shop.ypt_s_shop_cart')->with(['info'=>$info,'count'=>$count]);
    }

    /**
     * 购物车删除
     * @return mixed
     */
    public function CartDel()
    {
        $cart_id = $this->getParam('cart_id');
        $user_id = $this->getParam('user_id');
        //$user_id = session_get(UserEnum::USER_INFO_USER_ID);

        if(!empty($cart_id)){
            ShopService::instance()->CartDelOne($cart_id);
        }
        if(!empty($user_id)){
            ShopService::instance()->CartDelAll($user_id);
        }
        $info = ShopService::instance()->YptShopCart($user_id);
        $count = ShopService::instance()->YptShopCartCount($user_id);

        return $this->viewAjax('shop.ajaxTemplates.shop_del_part')->with(['info'=>$info,'count'=>$count]);
    }

    /**
     * 购物车添加
     */
    public function CartAdd()
    {

        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $paramObj                 = new stdClass();
        $paramObj->product_id     = $this->getParam('product_id');
        $paramObj->s_id           = $this->getParam('s_id');
        $paramObj->brand_id       = $this->getParam('brand_id');
        $paramObj->num            = $this->getParam('num');
        $paramObj->a_price        = $this->getParam('a_price');

        $info = ShopService::instance()->CartSel($paramObj->product_id,$paramObj->s_id,$paramObj->brand_id,$user_id);
        //print_R($info);die;
        if(!empty($info)){
            ShopService::instance()->CartUp($info,$paramObj,$user_id);
        }else{
            ShopService::instance()->CartAdd($paramObj,$user_id);
        }

    }



    /**
     * 央票通商城首页
     * @return mixed
     */
    public function YptShopIndex()
    {
//        #首页分类
//        $info = ShopService::instance()->CatesList();
//        foreach($info as $key =>$value){
//            $isess=ShopService::instance()->SelectCategory($value->c_id,$value->fid);
//            foreach($isess as $k =>&$v ){
//                $isess[$k]->sance=ShopService::instance()->SelectCategorys($v->c_id);
//            }
//            unset($v);
//            $value->cat_name=$isess;
//            $info[$key]=$value;
//        }
//      #导航栏
//        $daohang = ShopService::instance()->CatesList();
        #1f木材
//        $mucaiinfo = ShopService::instance()->MuCaiList();
//        foreach($mucaiinfo as $key =>$value){
//            $isess   =ShopService::instance()->SelectCategoryes($value->c_id,$value->fid);
//            foreach($isess as $k =>&$v ){
//                $isess[$k]->sance=ShopService::instance()->BusinList($v->c_id,$v->fid);
//            }
//            unset($v);
//            $value->cat_name=$isess;
//            $mucaiinfo[$key]=$value;
//        }
//        $ads = ShopService::instance()->AdvertPosit();


//        #分类下面的商家
        //$Busininfo= ShopService::instance()->BusinList();

        return $this->view('shop.ypt_shop_index_news');
    }


    function AdsShopCart(){
        $ads = ShopService::instance()->AdvertPosit();
        return $ads;
    }
}